package com.crack.nga2.fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.crack.nga2.R;
import com.crack.nga2.utils.GlobalConst;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NavigationFragment extends Fragment {
    private View rootView;
    private ListView listView;
    private String[] strs;
    private int[] imgs = {R.drawable.menu_forum, R.drawable.task_icon, R.drawable.menu_shop, R.drawable.menu_team
            , R.drawable.menu_scan, R.drawable.menu_setting};
    private TextView tvUserName;
    private ImageView ivPhoto;

    public NavigationFragment() {

    }

    public void setDrawerList(String[] strs) {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        strs = getActivity().getResources().getStringArray(R.array.navigation);
        rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
        listView = (ListView) rootView.findViewById(R.id.listView);
        listView.setAdapter(new NavigationAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SelectCallBacks callBacks = (SelectCallBacks) getActivity();
                callBacks.selectItem(parent, view, position);
            }
        });
        tvUserName = (TextView) rootView.findViewById(R.id.head_tv_name);
        ivPhoto = (ImageView) rootView.findViewById(R.id.head_photo);
        if (GlobalConst.isLogin) {
            initUserInfo();
        }
        return rootView;
    }

    public void initUserInfo(){
        tvUserName.setText(GlobalConst.username);
        ivPhoto.setImageResource(R.drawable.photo);
    }

    public void quite(){
        tvUserName.setText("点击登陆");
        ivPhoto.setImageResource(R.drawable.default_icon);
    }

    private class NavigationAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return strs.length;
        }

        @Override
        public Object getItem(int position) {
            return strs[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            view = View.inflate(getContext(), R.layout.fragment_navigation_item, null);
            ImageView iv = (ImageView) view.findViewById(R.id.navi_item_iv);
            TextView tv = (TextView) view.findViewById(R.id.navi_item_tv);
            iv.setImageResource(imgs[position]);
            tv.setText(strs[position]);
            return view;
        }
    }


    public interface SelectCallBacks {
        void selectItem(AdapterView<?> parent, View view, int position);
    }

}
